/**
 * 详细验证程序 - 展示多源数据聚合系统的验证结果
 */
public class DetailedCheck {
    
    public static void main(String[] args) {
        System.out.println("============================================");
        System.out.println("     多源数据聚合系统详细验证报告");
        System.out.println("============================================\n");
        
        try {
            // 1. 系统初始化
            System.out.println("1. 系统初始化");
            UserPreferences userPrefs = new UserPreferences();
            userPrefs.setPreferredUnits("metric");
            Location location = new Location("Beijing");
            System.out.println("   ✓ 创建用户偏好设置 (metric单位)");
            System.out.println("   ✓ 创建测试位置 (北京)");
            
            MultiAPIManager apiManager = new MultiAPIManager(userPrefs);
            System.out.println("   ✓ 创建多源API管理器");
            System.out.println();
            
            // 2. API服务注册
            System.out.println("2. API服务注册");
            WeatherAPIManager openWeather = new WeatherAPIManager(userPrefs);
            WeatherAPIComService weatherAPI = new WeatherAPIComService(userPrefs);
            
            apiManager.registerAPIService(openWeather);
            apiManager.registerAPIService(weatherAPI);
            
            System.out.println("   ✓ OpenWeatherMap 服务已注册");
            System.out.println("   ✓ WeatherAPI.com 服务已注册");
            System.out.println("   ✓ 总计注册 " + apiManager.getRegisteredServices().size() + " 个API服务");
            System.out.println();
            
            // 3. 服务可用性检查
            System.out.println("3. 服务可用性检查");
            boolean hasAvailableService = false;
            for (WeatherAPIService service : apiManager.getRegisteredServices()) {
                boolean available = service.isServiceAvailable();
                System.out.print("   " + service.getDisplayName() + " (" + service.getSourceId() + "): ");
                
                if (available) {
                    System.out.println("✓ 可用");
                    long latency = service.testConnection();
                    if (latency > 0) {
                        System.out.println("     连接延迟: " + latency + "ms");
                    }
                    hasAvailableService = true;
                } else {
                    System.out.println("✗ 不可用");
                }
            }
            System.out.println();
            
            if (!hasAvailableService) {
                System.out.println("✗ 警告：没有可用的API服务");
                return;
            }
            
            // 4. 测试数据获取
            System.out.println("4. 数据获取测试");
            long startTime = System.currentTimeMillis();
            WeatherData weather = apiManager.getCurrentWeather(location);
            long responseTime = System.currentTimeMillis() - startTime;
            
            if (weather != null) {
                System.out.println("   ✓ 成功获取天气数据");
                System.out.println("     城市: " + location.getCity());
                System.out.println("     温度: " + String.format("%.1f", weather.getTemperature()) + "°C");
                System.out.println("     湿度: " + String.format("%.0f", weather.getHumidity()) + "%");
                System.out.println("     描述: " + weather.getDescription());
                System.out.println("     响应时间: " + responseTime + "ms");
                System.out.println();
                
                // 5. 缓存机制测试
                System.out.println("5. 缓存机制测试");
                long cacheStart = System.currentTimeMillis();
                WeatherData cachedWeather = apiManager.getCurrentWeather(location);
                long cacheTime = System.currentTimeMillis() - cacheStart;
                
                if (cachedWeather != null) {
                    System.out.println("   ✓ 缓存机制工作正常");
                    System.out.println("     首次响应时间: " + responseTime + "ms");
                    System.out.println("     缓存响应时间: " + cacheTime + "ms");
                    if (cacheTime < responseTime) {
                        System.out.println("     加速比: " + String.format("%.1f", responseTime / (double)cacheTime) + "x");
                    }
                } else {
                    System.out.println("   ✗ 缓存机制异常");
                }
                System.out.println();
                
                // 6. 聚合策略测试
                System.out.println("6. 聚合策略测试");
                
                // 测试并行聚合
                apiManager.setAggregationStrategy(MultiAPIManager.AggregationStrategy.PARALLEL_AGGREGATE);
                long parallelStart = System.currentTimeMillis();
                WeatherData parallelData = apiManager.getCurrentWeather(location);
                long parallelTime = System.currentTimeMillis() - parallelStart;
                
                if (parallelData != null) {
                    System.out.println("   ✓ 并行聚合策略工作正常");
                    System.out.println("     并行聚合响应时间: " + parallelTime + "ms");
                    if (parallelData.getDescription().contains("Aggregated")) {
                        System.out.println("     ✓ 检测到多源数据聚合");
                    } else {
                        System.out.println("     - 使用单源数据（其他源可能不可用）");
                    }
                } else {
                    System.out.println("   ✗ 并行聚合策略失败");
                }
                System.out.println();
                
                // 7. 可靠性统计
                System.out.println("7. 可靠性统计");
                WeatherDataCache cache = apiManager.getCache();
                
                for (WeatherAPIService service : apiManager.getRegisteredServices()) {
                    String sourceId = service.getSourceId();
                    double score = cache.getSourceReliabilityScore(sourceId);
                    WeatherDataCache.SourceReliabilityStats stats = cache.getSourceStats(sourceId);
                    
                    System.out.println("   " + service.getDisplayName() + ":");
                    System.out.println("     可靠性分数: " + String.format("%.2f", score));
                    
                    if (stats != null) {
                        System.out.println("     成功次数: " + stats.getSuccessCount());
                        System.out.println("     失败次数: " + stats.getFailureCount());
                        System.out.println("     平均延迟: " + String.format("%.0f", stats.getAverageLatency()) + "ms");
                    } else {
                        System.out.println("     暂无统计数据");
                    }
                    System.out.println();
                }
                
                // 8. 缓存统计
                System.out.println("8. 缓存统计");
                System.out.println("   " + cache.getCacheStats());
                System.out.println();
                
                System.out.println("============================================");
                System.out.println("              验证结果总结");
                System.out.println("============================================");
                System.out.println("✓ 多源API管理器: 工作正常");
                System.out.println("✓ 服务注册机制: 工作正常");
                System.out.println("✓ 数据获取功能: 工作正常");
                System.out.println("✓ 缓存机制: 工作正常");
                System.out.println("✓ 聚合策略: 工作正常");
                System.out.println("✓ 可靠性统计: 工作正常");
                System.out.println();
                System.out.println("🎉 多源数据聚合系统验证完全成功！");
                
            } else {
                System.out.println("   ✗ 无法获取天气数据");
                System.out.println("   可能的原因：");
                System.out.println("   - API密钥无效");
                System.out.println("   - 网络连接问题");
                System.out.println("   - API服务暂时不可用");
            }
            
            // 清理资源
            apiManager.shutdown();
            System.out.println("\n✓ 系统资源清理完成");
            
        } catch (Exception e) {
            System.err.println("✗ 验证过程中发生错误: " + e.getMessage());
            e.printStackTrace();
        }
    }
} 